'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:LoginLoginCtrl
 * @description
 * # LoginLoginCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('LoginLoginCtrl', function($scope, openApi, Util,RongYService, $location, $http, $window, mdfive, UserInfo, DeveiceIdService, $timeout) {
        var userinfo = {};
        var clientType = Util.getPlatform() == "android" ? 1 : 2;
        userinfo = JSON.parse($window.localStorage.getItem("userinfo")) || {};
        $scope.saveuser = !!JSON.parse($window.localStorage.getItem("saveuser"));
        $scope.login = function() {
            if (!$scope.accountName) {
                Util.Toast("请输入组织名")
                return;
            }
            if (!$scope.userName) {
                Util.Toast("请输入用户名")
                return;
            }
            if (!$scope.passWord) {
                Util.Toast("请输入密码")
                return;
            }
            // 开始时候获取不到
            DeveiceIdService.getDeviceId().then(function(id) {
                if (!id) {
                    // 如果获取不到设备id,就过30秒钟再获取
                    $timeout(DeveiceIdService.sendIdToServe, 30000)
                }
                locallogin(id)
            })
        };
        var locallogin = function(id) {
                var params = {
                    clientType: clientType,
                    passWord: mdfive($scope.passWord),
                    userName: encodeURI($scope.userName),
                    accountName: encodeURI($scope.accountName)
                };
                if(id) {
                    params.clientId = id
                }
                $http.post(openApi.login, params).success(function(data, status, headers, config) {
                    userinfo.accountName = $scope.accountName;
                    userinfo.userName = $scope.userName;
                    userinfo.passWord = $scope.passWord;
                    $window.localStorage.setItem("userinfo", JSON.stringify(userinfo));
                    // 记住密码
                    if ($scope.saveuser) {
                        $window.localStorage.setItem("saveuser", JSON.stringify($scope.saveuser));
                    } else {
                        $window.localStorage.removeItem("saveuser");
                    }
                    if (data.success && data.result == "success") {
                        // 初始化融云服务
                        RongYService.init();
                        UserInfo.getUseinfo().then(function(data) {
                            // 保存用户基本信息
                            Util.saveBaseUserInfo(data.result);

                        }).finally(function() {
                            $location.path("index/index").replace()
                        });
                    } else {
                        Util.Toast(data.result)
                    }
                }).error(function(data, status, headers, config) {
                    // Util.Toast("请求接口失败!");
                });
            }
            // 记住密码是否自动登录
        if (!Util.isNullBbj(userinfo) && $scope.saveuser) {
            $scope.accountName = userinfo.accountName;
            $scope.userName = userinfo.userName;
            $scope.passWord = userinfo.passWord;
            $scope.login();
        }
    });
